<?php

class Navigation_Widgets_Navigation_Widget extends Nimblecms_Widget 
{
	private $_struct = null;
	private $_dump = null;
	
	protected function _prepareShow() 
	{
		$menu_id = $this->_request->getParam('menu_id');
		
		$query = Doctrine_Query::create()
				->from('Navigation_Model_Menuitem')
				->where('menu_id = ?', $menu_id)
				->orderBy('menu_id ASC')
				->addOrderBy('lft ASC')
				->addOrderBy('ordering ASC');
				
		$treeObject = Doctrine_Core::getTable('Navigation_Model_Menuitem')->getTree();
		$treeObject->setBaseQuery($query);
		$rootColumnName = $treeObject->getAttribute('rootColumnName');
		
		$items = array();
		
		$front = Zend_Controller_Front::getInstance();
		$uri = $front->getRequest()->getRequestUri();
		
                $lang = $front->getRequest()->getParam('lang');
                
                $baseUrl = $this->_view->baseUrl();
                
                $serverUrl = $this->_view->serverUrl();
                
		foreach ($treeObject->fetchRoots() as $root) {
		  $options = array(
		      'root_id' => $root->$rootColumnName
		  );
		  foreach($treeObject->fetchTree($options) as $key=>$node) {
		  		$node->getNode()->getParent() ? $items[$node->id]['parent'] = $node->getNode()->getParent()->id : $items[$node->id]['parent'] = null;
		  		$items[$node->id]['title'] = $node->Translation[$this->_view->LANG]->title;
		  		$items[$node->id]['level'] = $node->level;
		  		$items[$node->id]['visible'] = $node->published;
		   		
                                if($node->uri !== '#') {
                                    $path = substr($serverUrl . $node->uri, strlen($baseUrl));

                                    $items[$node->id]['uri'] = rtrim(substr($baseUrl, strlen($serverUrl)), '/') . '/' . $lang . '/' . ltrim($path, '/');
                                
                                } else {
                                    $items[$node->id]['uri'] = '#';
                                }
		   		$items[$node->id]['label'] = $node->Translation[$this->_view->LANG]->label;
		   		$node->target ? $items[$node->id]['target'] = '_self' : $items[$node->id]['target'] = '_blank';
		   		$items[$node->id]['id'] = $node->id;
				if($node->uri == $uri || strpos($uri, $node->uri) !== false) $items[$node->id]['active'] = true; else $items[$node->id]['active'] = false;
		  }
		}
		//Zend_Debug::dump($items);
		
		$treeObject->resetBaseQuery();
		
		
		
		foreach ($items as $item) {
			if($item['parent'] == NULL) {
				$this->_struct[$item['id']] = $item;
			} else {
				$this->_dump[$item['id']] = $item;
			}
		}
		
		//сборка контейнера
		$container = array();
		
		$container = $this->_gatherStruct($this->_struct);
		$container = $this->_clean($container);
		
		$nav = new Zend_Navigation($container);
		
		$this->_view->navigation = $nav;
		
	}
	
	private function _gatherStruct($struct) {
		if(count($struct) > 0) {
			foreach ($struct  as &$category) {
				$category['pages'] = array();
				
				if($this->_dump) {
					foreach ($this->_dump as $subcategory) {
						if($subcategory['parent'] == $category['id']) $category['pages'][$subcategory['id']] = $subcategory;
					}
				}
				if(count($category['pages'])>0) {
					$category['pages']=$this->_gatherStruct($category['pages']);
				}
			}
		}
		return $struct;
	}
	
	private function _clean($struct) {
		if(count($struct) > 0) {
			foreach ($struct as $id=>&$category) {
				unset($category['parent']);
				
				if(count($category['pages']) == 0) {
					unset($category['pages']);
				} else {
					$struct[$id]['pages'] = $this->_clean($category['pages']);
				}
			}
		}
		return $struct;
	}
}
